Repository navigation
Support a deletedAt timestamp as the logical delete marker - #17
Conversation
Add the LogicalDeleteTimestamp trait, which records when an entity was logically deleted in a nullable deletedAt Instant instead of a boolean flag. - delete() sets deletedAt to the current time, undelete() clears it and delete(hard: true) still removes the entity - PreQueryListener excludes entities with deletedAt set from query results, and withDeleted and @WithDeleted include them, as for LogicalDelete - Add a Customer example with integration tests, run against both GORM for Hibernate 5 and Hibernate 7, and document the trait Closes #16
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
No blocking issues were identified, and integration coverage addresses the main behaviors, though tests were not executed during review.
Review effort: Balanced
Findings: None
What changed in this PR
Adds timestamp-based logical deletion alongside the existing boolean-based LogicalDelete trait, addressing #16 without changing its behavior.
Changes:
- Adds
LogicalDeleteTimestampwith a nullableInstant deletedAtmarker. - Extends query filtering to exclude timestamp-deleted records.
- Adds documentation, examples, and integration coverage.
| File | Description |
|---|---|
| plugin/src/main/groovy/grails/logical/delete/PreQueryListener.groovy | Selects filtering criteria for either deletion marker. |
| plugin/src/main/groovy/grails/logical/delete/LogicalDeleteTimestamp.groovy | Implements timestamp deletion, restoration, and filtered queries. |
| plugin/src/docs/index.adoc | Documents timestamp usage and nullability requirements. |
| examples/app1/src/integration-test/groovy/grails/logical/delete/LogicalDeleteTimestampSpec.groovy | Covers deletion, filtering, restoration, mapping, and Data Services. |
| examples/app1/grails-app/services/app/CustomersDataService.groovy | Demonstrates Data Service queries with @WithDeleted. |
| examples/app1/grails-app/domain/app/Customer.groovy | Demonstrates the trait and custom column mapping. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Review finding (low confidence) —
|
Deleting an entity that is already logically deleted no longer overwrites deletedAt, so the original deletion time is not lost.
|
Good catch, thanks! With a timestamp, overwriting the original deletion time on a repeated Added a test for it and a note in the docs in d51395e. |
Closes #16
Adds a
LogicalDeleteTimestamptrait, which records when an entity was logically deleted, in a nullabledeletedAttimestamp, instead of the booleandeletedflag.delete()setsdeletedAtto the current time,undelete()clears it, anddelete(hard: true)still removes the row.deletedAtset are excluded fromget/read/load/proxy,list/findAll, dynamic finders, where queries and criteria (including projections).withDeleted { }and@WithDeleted, including on Data Service methods, include them.deletedAtcan be mapped like any other property, e.g.deletedAt(column: 'removed_at').The existing
LogicalDeletetrait is unchanged.Changes
LogicalDeleteTimestamp: new trait mirroringLogicalDelete, withisNull('deletedAt')in place ofdeleted == false.PreQueryListener: picks the "not deleted" condition per trait (deleted = falseordeletedAt IS NULL).Customerdomain class,CustomersDataServiceandLogicalDeleteTimestampSpec. The spec runs against both GORM for Hibernate 5 (app1) and Hibernate 7 (app1-hibernate7).Decisions on the open questions in #16
LogicalDeleteTimestamp/deletedAt.java.time.Instant, as an unambiguous point in time. Not configurable through a type parameter.Instant.now(). No configurableClockbean.Notes
deletedAtneeds no constraint. Apps that make properties non-nullable by default needdeletedAt(nullable: true), which the docs mention. A trait cannot contributeconstraintsitself.LogicalDelete(withDeleted, the flag accessors, the static API lookup), to leaveLogicalDeleteuntouched. They could move to a shared internal class later.